package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TBaseCost;
import com.ruoyi.system.service.ITBaseCostService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 费用类别Controller
 * 
 * @author ruoyi
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/system/cost")
public class TBaseCostController extends BaseController
{
    @Autowired
    private ITBaseCostService tBaseCostService;

    /**
     * 查询费用类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBaseCost tBaseCost)
    {
        startPage();
        List<TBaseCost> list = tBaseCostService.selectTBaseCostList(tBaseCost);
        return getDataTable(list);
    }

    /**
     * 导出费用类别列表
     */
    @PreAuthorize("@ss.hasPermi('system:cost:export')")
    @Log(title = "费用类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBaseCost tBaseCost)
    {
        List<TBaseCost> list = tBaseCostService.selectTBaseCostList(tBaseCost);
        ExcelUtil<TBaseCost> util = new ExcelUtil<TBaseCost>(TBaseCost.class);
        util.exportExcel(response, list, "费用类别数据");
    }

    /**
     * 获取费用类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:cost:query')")
    @GetMapping(value = "/{costId}")
    public AjaxResult getInfo(@PathVariable("costId") Long costId)
    {
        return success(tBaseCostService.selectTBaseCostByCostId(costId));
    }

    /**
     * 新增费用类别
     */
    @PreAuthorize("@ss.hasPermi('system:cost:add')")
    @Log(title = "费用类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBaseCost tBaseCost)
    {
        Long userId = SecurityUtils.getUserId();
        tBaseCost.setCreatePerson(userId.toString());
        tBaseCost.setCreateDate(DateUtils.getNowDate());
        return toAjax(tBaseCostService.insertTBaseCost(tBaseCost));
    }

    /**
     * 修改费用类别
     */
    @PreAuthorize("@ss.hasPermi('system:cost:edit')")
    @Log(title = "费用类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBaseCost tBaseCost)
    {
        Long userId = SecurityUtils.getUserId();
        tBaseCost.setUpdatePerson(userId.toString());
        tBaseCost.setUpdateDate(DateUtils.getNowDate());
        return toAjax(tBaseCostService.updateTBaseCost(tBaseCost));
    }

    /**
     * 删除费用类别
     */
    @PreAuthorize("@ss.hasPermi('system:cost:remove')")
    @Log(title = "费用类别", businessType = BusinessType.DELETE)
	@DeleteMapping("/{costIds}")
    public AjaxResult remove(@PathVariable Long[] costIds)
    {
        return toAjax(tBaseCostService.deleteTBaseCostByCostIds(costIds));
    }
}
